package com.house365.core.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;

import com.house365.core.R;

public class UISwitch extends LinearLayout implements OnTouchListener {
	// 四个动画
	private Animation slideLeftIn;
	private Animation slideLeftOut;
	private Animation slideRightIn;
	private Animation slideRightOut;
	private Context context;
	private ViewFlipper viewFlipper;
	// 判断手势用
	private static final int SWIPE_MIN_DISTANCE = 10;// 滑动的最小值
	// private static final int SWIPE_MAX_OFF_PATH = 250;// 滑动的最大值
	// private static final int SWIPE_THRESHOLD_VELOCITY = 200;// 滑动的阈值
	private ImageView openImage;
	private ImageView closeImage;
	private GestureDetector mGesture = null;

	public UISwitch(Context context) {
		super(context);
		this.context = context;
		init(context);
		mGesture = new GestureDetector(context, new GestureListener());
	}

	public UISwitch(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// setNextViewFlipper();
		return mGesture.onTouchEvent(event);
	}

	private void init(Context context) {

		RelativeLayout.LayoutParams params_cal = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		viewFlipper = new ViewFlipper(context);
		openImage = new ImageView(context);
		openImage.setBackgroundResource(R.drawable.on);
		closeImage = new ImageView(context);
		closeImage.setBackgroundResource(R.drawable.off);
		viewFlipper.addView(openImage);
		viewFlipper.addView(closeImage);
		openImage.setOnTouchListener(this);
		closeImage.setOnTouchListener(this);
		openImage.setLongClickable(true);
		closeImage.setLongClickable(true);
		addView(viewFlipper, params_cal);
		setAnimation();
	}

	// 设置动画
	private void setAnimation() {
		slideLeftIn = AnimationUtils.loadAnimation(context,
				R.anim.slide_in_left);
		slideLeftOut = AnimationUtils.loadAnimation(context,
				R.anim.slide_out_left);
		slideRightIn = AnimationUtils.loadAnimation(context,
				R.anim.slide_in_right);
		slideRightOut = AnimationUtils.loadAnimation(context,
				R.anim.slide_out_right);

		slideLeftIn.setAnimationListener(animationListener);
		slideLeftOut.setAnimationListener(animationListener);
		slideRightIn.setAnimationListener(animationListener);
		slideRightOut.setAnimationListener(animationListener);
	}

	private AnimationListener animationListener = new AnimationListener() {
		@Override
		public void onAnimationStart(Animation animation) {
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			// 当动画完成后调用
			// CreateGirdView();
		}
	};

	class GestureListener extends SimpleOnGestureListener {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			try {
				// if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
				// return false;
				// right to left swipe
				if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && isOpen()) {
					setNextViewFlipper();
					return true;

				} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
						&& !isOpen()
				// && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
				) {
					setLastViewFlipper();
					return true;

				}
			} catch (Exception e) {
				// nothing
			}
			return false;
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {

			return false;

		}// 当手指抬起来的时候

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			// TODO Auto-generated method stub
			return super.onDoubleTap(e);
		}

		@Override
		public boolean onDoubleTapEvent(MotionEvent e) {
			// TODO Auto-generated method stub
			return super.onDoubleTapEvent(e);
		}

		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			return super.onDown(e);
		}

		@Override
		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			super.onLongPress(e);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub
			return super.onScroll(e1, e2, distanceX, distanceY);
		}

		@Override
		public void onShowPress(MotionEvent e) {
			// TODO Auto-generated method stub
			super.onShowPress(e);
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			// TODO Auto-generated method stub
			return super.onSingleTapConfirmed(e);
		}

	}

	private void setNextViewFlipper() {// 显示下一个视图
		viewFlipper.setInAnimation(slideLeftIn);
		viewFlipper.setOutAnimation(slideLeftOut);
		viewFlipper.showNext();
	}

	private void setLastViewFlipper() {// 显示下一个视图
		viewFlipper.setInAnimation(slideRightIn);
		viewFlipper.setOutAnimation(slideRightOut);
		viewFlipper.showPrevious();
	}

	public boolean isOpen() {// 判断是否打开
		ImageView view = (ImageView) viewFlipper.getCurrentView();
		if (view == openImage) {
			return true;
		}
		return false;
	}

	public void setOpenImage(ImageView openImage) {
		this.openImage = openImage;
	}

	public void setCloseImage(ImageView closeImage) {
		this.closeImage = closeImage;
	}

}
